home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / bsfengines.jar / com / ibm / bsf / engines / javascript / JavaScriptEngine.class (.txt)
Encoding:
Java Class File  |  1999-12-09  |  3.8 KB  |  130 lines

  1. package com.ibm.bsf.engines.javascript;
  2.  
  3. import com.ibm.bsf.BSFDeclaredBean;
  4. import com.ibm.bsf.BSFException;
  5. import com.ibm.bsf.BSFManager;
  6. import com.ibm.bsf.util.BSFEngineImpl;
  7. import com.ibm.bsf.util.BSFFunctions;
  8. import java.util.Vector;
  9. import org.mozilla.javascript.Context;
  10. import org.mozilla.javascript.EvaluatorException;
  11. import org.mozilla.javascript.JavaScriptException;
  12. import org.mozilla.javascript.ScriptRuntime;
  13. import org.mozilla.javascript.Scriptable;
  14. import org.mozilla.javascript.ScriptableObject;
  15. import org.mozilla.javascript.WrappedException;
  16. import org.mozilla.javascript.Wrapper;
  17.  
  18. public class JavaScriptEngine extends BSFEngineImpl {
  19.    private Scriptable global;
  20.    // $FF: renamed from: cx org.mozilla.javascript.Context
  21.    private Context field_0;
  22.  
  23.    public void initialize(BSFManager var1, String var2, Vector var3) throws BSFException {
  24.       super.initialize(var1, var2, var3);
  25.  
  26.       try {
  27.          this.field_0 = new Context();
  28.          Context.enter();
  29.          this.global = this.field_0.initStandardObjects((ScriptableObject)null);
  30.          this.global.put("bsf", this.global, new BSFFunctions(var1, this));
  31.       } finally {
  32.          if (this.field_0 != null) {
  33.             this.field_0.exit();
  34.          }
  35.  
  36.       }
  37.  
  38.       int var4 = var3.size();
  39.  
  40.       for(int var5 = 0; var5 < var4; ++var5) {
  41.          this.declareBean((BSFDeclaredBean)var3.elementAt(var5));
  42.       }
  43.  
  44.    }
  45.  
  46.    public Object eval(String var1, int var2, int var3, Object var4) throws BSFException {
  47.       String var5 = var4.toString();
  48.       Object var6 = null;
  49.  
  50.       try {
  51.          Context.enter();
  52.          var6 = this.field_0.evaluateString(this.global, var5, var1, var2, (Object)null);
  53.       } catch (Throwable var12) {
  54.          this.handleError(var12);
  55.       } finally {
  56.          this.field_0.exit();
  57.       }
  58.  
  59.       return var6;
  60.    }
  61.  
  62.    public Object call(Object var1, String var2, Object[] var3) throws BSFException {
  63.       Object var4 = null;
  64.  
  65.       try {
  66.          Context.enter();
  67.          Object var7 = this.global.get(var2, this.global);
  68.          if (var7 == Scriptable.NOT_FOUND) {
  69.             throw new JavaScriptException("function " + var2 + " not found.");
  70.          }
  71.  
  72.          var4 = ScriptRuntime.call(this.field_0, var7, this.global, var3);
  73.          if (var4 instanceof Wrapper) {
  74.             var4 = ((Wrapper)var4).unwrap();
  75.          }
  76.       } catch (Throwable var10) {
  77.          this.handleError(var10);
  78.       } finally {
  79.          this.field_0.exit();
  80.       }
  81.  
  82.       return var4;
  83.    }
  84.  
  85.    public void declareBean(BSFDeclaredBean var1) throws BSFException {
  86.       this.global.put(var1.name, this.global, var1.bean);
  87.    }
  88.  
  89.    public void undeclareBean(BSFDeclaredBean var1) throws BSFException {
  90.       this.global.put(var1.name, this.global, (Object)null);
  91.    }
  92.  
  93.    private void handleError(Throwable var1) throws BSFException {
  94.       if (var1 instanceof WrappedException) {
  95.          var1 = (Throwable)((WrappedException)var1).unwrap();
  96.       }
  97.  
  98.       String var2 = null;
  99.       Throwable var3 = null;
  100.       if (var1 instanceof JavaScriptException) {
  101.          var2 = var1.getLocalizedMessage();
  102.          Object var4 = ((JavaScriptException)var1).getValue();
  103.          if (var4 instanceof Throwable) {
  104.             var3 = (Throwable)var4;
  105.          }
  106.       } else if (!(var1 instanceof EvaluatorException) && !(var1 instanceof SecurityException)) {
  107.          if (var1 instanceof RuntimeException) {
  108.             var2 = "Internal Error: " + var1.toString();
  109.             var3 = var1;
  110.          } else if (var1 instanceof StackOverflowError) {
  111.             var2 = "Stack Overflow";
  112.          } else {
  113.             var3 = var1;
  114.          }
  115.       } else {
  116.          var2 = var1.getLocalizedMessage();
  117.       }
  118.  
  119.       if (var2 == null) {
  120.          var2 = var1.toString();
  121.       }
  122.  
  123.       if (var1 instanceof Error && !(var1 instanceof StackOverflowError)) {
  124.          throw (Error)var1;
  125.       } else {
  126.          throw new BSFException(BSFException.REASON_OTHER_ERROR, "JavaScript Error: " + var2, var3);
  127.       }
  128.    }
  129. }
  130.